package com.css.fxfzaqbz.modules.assess2.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.KrTask;
import com.css.fxfzaqbz.modules.assess2.entity.UTask;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.assess2.repository.UTaskRepository;
import com.css.fxfzaqbz.modules.assess2.repository.UrgencysettleDataRepository;
import com.css.fxfzaqbz.modules.assess2.service.UTaskService;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UTaskServiceImpl implements UTaskService {
    @Resource
    ImportantActivityService importantActivityService;
    @Resource
    UTaskRepository uTaskRepository;
    @Resource
    UrgencysettleDataRepository urgencysettleDataRepository;

    @Override
    public UTask getTask(String id) {
        //判断数据库里是否已经存在这一个大表
        UTask uTask = uTaskRepository.getTask(id);
        ImportantActivity importantActivity = importantActivityService.getImportantActivityById(id);
        if (uTask != null) {
            //已经存在
            uTask.setUtActiveid(id);
            uTask.setUtId(UUIDGenerator.getUUID());
            uTask.setKtOpinionStatus(importantActivity.getIaOpinionStatus());
            uTask.setUtOpinion(importantActivity.getIaOpinion());
            UrgencysettleData urgencysettleData = urgencysettleDataRepository.getUrgencysettleData(id);
            if (urgencysettleData == null) {
                List<UrgencysettleData> list = new ArrayList<>();
                uTask.setLists(list);
                return uTask;
            }
            List<UrgencysettleData> list = new ArrayList<>();
            list.add(urgencysettleData);
            uTask.setLists(list);
            return uTask;
        } else {
            //不存在
            UTask uTask1 = new UTask();
            List<UrgencysettleData> list = new ArrayList<>();
            uTask1.setLists(list);
            uTask1.setKtOpinionStatus(importantActivity.getIaOpinionStatus());
            uTask1.setUtOpinion(importantActivity.getIaOpinion());
            return uTask1;
        }
    }

    @Override
    public void saveTask(ImportantActivity importantActivity1) {
        UTask task = uTaskRepository.getTask(importantActivity1.getIaId());
        if (task == null) {
            UTask uTask = new UTask();
            uTask.setUtId(UUIDGenerator.getUUID());
            uTask.setContent("需紧急安置人员");
            uTask.setUtStatus(YNEnum.N.toString());
            uTask.setUtActiveid(importantActivity1.getIaId());
            uTask.setUtDelFlag(YNEnum.N.toString());
            uTask.setUtCreateTime(PlatformDateUtils.getCurrentTimestamp());
            uTask.setUtCreateUser(PlatformSessionUtils.getUserId());
            uTask.setUtOpinion(importantActivity1.getIaOpinion());
            uTaskRepository.saveTask(uTask);
        }

    }

    @Override
    public void upStatus(String id) {
        uTaskRepository.upStatus(id);

    }
}
